<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-friendadd"></i>账号审核
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" @click="ALL" class="lkx_r20">全部</el-button>
        <el-input
          v-model.trim="select_word"
          placeholder="筛选关键词"
          class="handle-input mr10"
          @keyup.enter.native="search_key(select_word)"
        ></el-input>
        <el-button type="primary" icon="search" @click="search_key(select_word)">搜索</el-button>
      </div>
      <el-table
        border
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column prop="name" label="账号名称" align="center"></el-table-column>
        <el-table-column prop="accountType" label="账号类型" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center"></el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">{{ scope.row.date }}</template>
        </el-table-column>
        <el-table-column label="申请时间" align="center">
          <template slot-scope="scope">{{ scope.row.applyDate }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination_div">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Totals"
          :page-size="10"
          :page-count="pageTotal"
          :current-page.sync="currPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import * as api from "../../http/api.js";
export default {
  data() {
    return {
      //表格数据
      tableData: [
        // {
        //   name: "",
        //   accountType:null,
        //   status: "",
        //   date: "",
        //   applyDate: ""
        // }
      ],
      bfdata: [], //备份数据
      currentRow: null,
      select_word: "", //搜索的关键词
      editVisible: false, //查看详情开关
      form: {
        name: "",
        accountType: ""
      },
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //分页页码
      Totals: 1, //总条数 默认为1条，在获取接口列表时在传递过来，改变1
      pageTotal: 1, //总页数
      Media_id: "" //媒体id 用来连接详情的参数
    };
  },
  methods: {
    getData(key) {
      //获得表格数据
      this.$post(api.media_review, {
        token: this.Token,
        user_name: key,
        page: this.currPage
      })
        .then(res => {
          if (res.status == "0") {
            this.tableData = res.data.map((val, index, arr) => {
              let jsons = [];
              jsons.name = val.user_name; //账号名称
              jsons.accountType = val.type; //账号类型
              jsons.status = val.status; //状态 1启用，2 禁用
              jsons.date = val.add_time; //创建时间
              jsons.applyDate = val.apply_time; //申请时间
              jsons.media_id = val.media_id; //媒体id
              return jsons;
            });

            this.tableData.forEach(val => {
              if (val.accountType == 1) {
                val.accountType = "个人";
              } else {
                val.accountType = "组织";
              }
              if (val.status == 1) {
                val.status = "已审核 ";
              } else {
                val.status = "待审核";
              }
            });
            // 获取总条数和总页数
            this.Totals = res.total;
            this.pageTotal = res.pageTotal;
            this.currPage = res.currPage;
          } else {
            console.log(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    search_key(select_word) {
      if (select_word == "") {
        //  如果搜索为空，弹出提示框
        this.$message.error(`搜索不能为空`);
        return;
      } else {
        this.currPage = 1;
        this.getData(this.select_word);
      }
    },
    //查看详情页面
    handleEdit(index, row) {
      //如果行里面的accountType 是个人就跳转到个人页面，企业就跳转企业
      this.Media_id = this.tableData[index].media_id;
      if (row.accountType == "个人") {
        this.$router.push({
          path: "/personInfo",
          query: { id: this.Media_id }
        });
      } else {
        this.$router.push({
          path: "/enterpriseInfo",
          query: { id: this.Media_id }
        });
      }
    },
    //点击分页
    handleCurrentChange(val) {
      this.currPage = val;
      this.getData(this.select_word);
    },
    ALL() {
      this.currPage = 1;
      this.select_word = "";      
      this.getData(this.select_word);
    }
  },
  created() {
    this.getData();
  }
};
</script>
<style scoped>
.mr10 {
  margin-right: 10px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.handle-box {
  padding-bottom: 20px;
}
.pagination_div {
  margin-top: 20px;
  text-align: right;
}
</style>